package com.anysoft.visitdd.example;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.UUID;
import java.util.logging.Logger;

public class OwnerClient {
    private static final String USER_PUB_KEY = "2d2d2d2d2d424547494e205055424c4943204b45592d2d2d2d2d0d4d4947664d413047435371475349623344514542415155414134474e4144434269514b42675144327a474d59774a69714b2b467a74763452514331676e31544b0d426d4d37585376756b443570446f44304d2b50704f363461546c2f6361764e526e4e6e58347975464e396231626a77634a435259744546776474656e765954670d2b6b65496268746e5a78586b6e73444d73576d694e7166397a6e6a5768362f6e657746452f6168633565526578702f585446583857654f444e762f4a787038510d4b6d67382b46665163695452674c745168774944415141420d2d2d2d2d2d454e44205055424c4943204b45592d2d2d2d2d0d";

    static void queryRecords() throws Exception {
        var gson = new Gson();
        var rsaEnc = new RsaEncrypter(ProjectSecurityCommon.ALG_RSA_FOR_ANDROID, App.SERVER_PUB_KEY);
        var j = new JsonObject();
        var fields = new JsonArray();
        fields.add("lbl");
        fields.add("num");
        j.add("fields", fields);
        j.addProperty("pageIndex", 1);
        j.addProperty("pageSize", 100);
        var msg = gson.toJson(j);
        System.out.println("msg");
        System.out.println(msg);
        var msgEncd = rsaEnc.encrypt(msg);
        try (var hc = HttpClient.newBuilder().build()) {
            var req = App.fillToken(USER_PUB_KEY).uri(URI.create(App.API_HEAD + "/gateway/page/LabelNumber"))
                    .method("GET", HttpRequest.BodyPublishers.ofString(msgEncd)).build();
            var res = hc.send(req, HttpResponse.BodyHandlers.ofString());
            var resBody = res.body();
            var parser = new JsonParser();
            var arm = parser.parse(resBody).getAsJsonObject();
            Logger.getGlobal().info(arm.toString());
        }
    }

    static JsonObject removeRecord(long id) {
        var value = new JsonObject();
        value.addProperty("action","DR");
        value.addProperty("id", id);
        return value;
    }

    static JsonObject removeField(long id, String tag) {
        var value = new JsonObject();
        value.addProperty("action","DFV");
        value.addProperty("id", id);
        value.addProperty("tag", tag);
        return value;
    }

    static JsonObject changeField(long id, String tag,String fv) {
        var value = new JsonObject();
        value.addProperty("action","UFV");
        value.addProperty("id", id);
        value.addProperty("tag", tag);
        value.addProperty("fv", fv);
        return value;
    }

    static void addRecords(int count) throws Exception {
        var gson = new Gson();
        var rsaEnc = new RsaEncrypter(ProjectSecurityCommon.ALG_RSA_FOR_ANDROID, App.SERVER_PUB_KEY);
        var j = new JsonObject();
        var fields = new JsonArray();
        fields.add("lbl");
        fields.add("num");
        j.add("fields", fields);
        var records = new JsonArray();
        for (int i = 0; i < count; i++) {
            var value = new JsonObject();
            value.addProperty("lbl", UUID.randomUUID().toString());
            value.addProperty("num", Math.random() * 10000);
            records.add(value);
        }
        j.add("records", records);
        var msg = gson.toJson(j);
//        System.out.println("msg");
//        System.out.println(msg);
        var msgEncd = rsaEnc.encrypt(msg);
        try (var hc = HttpClient.newBuilder().build()) {
            var req = App.fillToken(USER_PUB_KEY).uri(URI.create(App.API_HEAD + "/gateway/batch-add/LabelNumber"))
                    .POST(HttpRequest.BodyPublishers.ofString(msgEncd)).build();
            var res = hc.send(req, HttpResponse.BodyHandlers.ofString());
            var resBody = res.body();
            var parser = new JsonParser();
            var arm = parser.parse(resBody).getAsJsonObject();
            Logger.getGlobal().info(arm.toString());
        }
    }

    static void changeRecords() throws Exception {
        var gson = new Gson();
        var rsaEnc = new RsaEncrypter(ProjectSecurityCommon.ALG_RSA_FOR_ANDROID, App.SERVER_PUB_KEY);
        var j = new JsonObject();
        var actions = new JsonArray();
//        actions.add(removeRecord(1780202764041L));
//        actions.add(removeRecord(1780202742464L));
//        actions.add(removeRecord(1780202746710L));
//        actions.add(removeRecord(1780202765728L));
//        actions.add(changeField(3564479990459L, "num", 77));
//        actions.add(removeField(1780202758977L, "lbl"));
        actions.add(changeField(1780202758977L, "lbl", "ChangedValueFromClient"));
        actions.add(changeField(3564480006766L, "lbl", "ChangedValueFromClient"));
        actions.add(changeField(1780202765728L, "lbl", "ChangedValueFromClient"));
        actions.add(changeField(1780159372630L, "lbl", "ChangedValueFromClient"));

        j.add("actions", actions);
        var msg = gson.toJson(j);
//        System.out.println("msg");
//        System.out.println(msg);
        var msgEncd = rsaEnc.encrypt(msg);
        try (var hc = HttpClient.newBuilder().build()) {
            var req = App.fillToken(USER_PUB_KEY).uri(URI.create(App.API_HEAD + "/gateway/batch-modify/LabelNumber"))
                    .POST(HttpRequest.BodyPublishers.ofString(msgEncd)).build();
            var res = hc.send(req, HttpResponse.BodyHandlers.ofString());
            var resBody = res.body();
            var parser = new JsonParser();
            var arm = parser.parse(resBody).getAsJsonObject();
            Logger.getGlobal().info(arm.toString());
        }
    }
}
